<template>
  <el-dialog
    v-model="dialogFormVisible"
    :title="title"
    width="700px"
    @close="close"
  >
    <el-form ref="formRef" label-width="200px" :model="form" :rules="rules">
      <el-form-item v-if="edittype == 0" label="小记内容" prop="title">
        <el-input v-model="form.title" />
      </el-form-item>
      <el-form-item label="上传截图" prop="title">
        <el-upload
          v-model:file-list="fileList"
          action="https://run.mocky.io/v3/9d059bf9-4660-45f2-925d-ce80ad6c4d15"
          list-type="picture-card"
          :on-preview="handlePictureCardPreview"
          :on-remove="handleRemove"
        >
          <el-icon><Plus /></el-icon>
        </el-upload>
      </el-form-item>
      <el-form-item v-if="edittype == 1" label="统计月份" prop="title">
        <el-input v-model="form.title" />
      </el-form-item>
      <el-form-item
        v-if="edittype == 1"
        label="是否达标行业优选商家"
        prop="title"
      >
        <el-select placeholder="">
          <el-option value="" />
        </el-select>
      </el-form-item>
      <el-form-item v-if="edittype == 1" label="金冠商品数" prop="title">
        <el-input v-model="form.title" />
      </el-form-item>
      <el-form-item v-if="edittype == 1" label="上月在线商品数" prop="title">
        <el-input v-model="form.title" />
      </el-form-item>
      <el-form-item v-if="edittype == 1" label="展现次数" prop="title">
        <el-input v-model="form.title" />
      </el-form-item>
      <el-form-item v-if="edittype == 1" label="访客数" prop="title">
        <el-input v-model="form.title" />
      </el-form-item>
      <el-form-item v-if="edittype == 1" label="浏览量" prop="title">
        <el-input v-model="form.title" />
      </el-form-item>
      <el-form-item v-if="edittype == 1" label="询盘量" prop="title">
        <el-input v-model="form.title" />
      </el-form-item>
    </el-form>
    <el-dialog v-model="dialogVisible">
      <img w-full :src="dialogImageUrl" alt="Preview Image" />
    </el-dialog>
    <template #footer>
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </template>
  </el-dialog>
</template>

<script>
  import { doEdit } from '@/api/storeDetails'

  export default defineComponent({
    name: 'StoreDetailsEdit',
    emits: ['fetch-data'],
    setup(props, { emit }) {
      const $baseMessage = inject('$baseMessage')

      const state = reactive({
        formRef: null,
        form: {
          roles: [],
        },
        rules: {
          title: [{ required: true, trigger: 'blur', message: '请输入标题' }],
        },
        edittype: 0,
        title: '',
        dialogFormVisible: false,
      })

      const showEdit = (n, row) => {
        state.edittype = n
        if (!row) {
          state.title = '添加小记'
        } else {
          state.title = '编辑小记'
          state.form = Object.assign({}, row)
        }
        state.dialogFormVisible = true
      }
      const close = () => {
        state['formRef'].resetFields()
        state.form = {
          roles: [],
        }
        state.dialogFormVisible = false
      }
      const save = () => {
        state['formRef'].validate(async (valid) => {
          if (valid) {
            const { msg } = await doEdit(state.form)
            $baseMessage(msg, 'success', 'vab-hey-message-success')
            emit('fetch-data')
            close()
          }
        })
      }

      return {
        ...toRefs(state),
        showEdit,
        close,
        save,
      }
    },
  })
</script>
